<?php

namespace Modules\Base\Repository;

use Modules\Base\Models\OrderNumber;

/**
 * 单据编号相关
 * Class OrderNumberRepository
 * @package Modules\Base\Repository
 */
class OrderNumberRepository
{

    /**
     * 根据单据类型获取单据序号
     * @param $prefix
     * @return int
     */
    static public function getOrderNumberByPrefix($prefix){
        $today = date('Ymd');
        $number = OrderNumber::where('prefix',$prefix)->where('today',$today)->first();

        $no = 1;
        if(!$number || $number->today != $today){
            OrderNumber::insert([
                'prefix' => $prefix,
                'today' => $today,
                'num'   => $no
            ]);
        }else{
            $no = $number->num + 1;
            $number->increment('num',1);
        }
        return $no;
    }

}
